#!/usr/bin/perl 

#use warnings;
use strict;

use POSIX;

unless (defined $ARGV[0])
	{
	print "Usage: conv_8b_pgm_to_4b_array.pl [FILE]\n";
	print "Argument must be a 8bit PGM file. Gimp produces these quite nicely.\n";
	print "NOTE: You MUST save the PGM file in \"ASCII\" mode, not \"RAW\" for this to work\n";
	exit(0);
	}

open(FILE, "<", $ARGV[0]) or
	die "8bit PGM (ASCII) file $ARGV[0] couldn't be opened. Reason: $!";


my $ARRAY_NAME = $ARGV[0];
$ARRAY_NAME =~ s/\..*//;


my @ok_array;
my @smart_array;

my $i;
my $test;
my $data = 0;
my $columncounter = 0;


#print "      \t 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26"; # Debug debug
#print "\n 1 =\t";	# Debug debug

while(<FILE>)
	{
		$i++;
		next if($i < 5);
		
		print $_ if ($i == 4);
		chomp($ok_array[$data] = $_);
		
		$ok_array[$data] = $ok_array[$data] >> 4;
		#print "0" if ($ok_array[$data] < 10);	# Debug debug
		#print $ok_array[$data].","; # Debug debug
		
		# Here we add padding - a 0 after each 25th element
		if ($columncounter == 24) {	# The 25th column
			$data++;				# jump to the 26th
			$ok_array[$data] = 0;
			
			#print $ok_array[$data].","; # Debug debug
			$test = ceil(($data -4) /24)+1;
			#print "\n $test =\t" if ($data > 0); # Debug debug
			$columncounter = -1;
			}
		
		$columncounter++;
		
		
		#print "\n $i\t" if (!($data % 24) && ($data));
		$data++;
	}
	#print "\n\nGot array with $data elements - padded with 0 after each 25th element\n\n";	# Debug debug
# Ok, array populated



my $column = 1;
my $first_half = 1;
my $half_counter=0;
my $smart_counter = 0;

#print "We now got the 4bit values, stored in a byte each....\n\n\n";
		# print "\n Starting process of truncting it into one byte with two 4bit pairs...\n";	# Debug debug
		my $array_length = @ok_array;
		#print "the \@ok_array should now be 26*25 = 650 elements, got $array_length\n";	# Debug debug

for (my $i = 0; $i < @ok_array; $i++) {	# Here we got the 4bit values, stored in one byte each

		# Print it out for debugging
		#print "0" if ($ok_array[$i] < 10);	# Debug debug
		#print $ok_array[$i]." ";			# Debug debug
		
		
		$column++;
		#print ("\n") if ($column == 27);	# Debug debug
		$column = 1 if ($column > 26);
		
		$first_half = 1;
		$first_half = 0 if($half_counter==1);
		
		
		# Time to truncate it into bytes
		# When we're on 
		if($first_half) { 	# We're getting the 4 most significant bits - first half of the byte
			$smart_array[$smart_counter] = $ok_array[$i] << 4 | $ok_array[$i+1];
			$smart_counter++;
			} else {		# We're on the second half, so we skip quietly
			}

		$half_counter++;
		$half_counter=0 if($half_counter ==2);
	}


my $x = 0;
my $y = 0;
print "\nPROGMEM char ".$ARRAY_NAME."[13*25] =	// 13*25 = 325	// Saved in program (flash) memory to save RAM\n";
print "\t{\n\t";

foreach(@smart_array) {
	
	print " " if ($_ <10);		# Debug debug
	print " " if ($_ <100);	# Debug debug
	print $_;
	print "," unless($y == 324);
	print " " unless($x == 13);
	$x++;
	$y++;
	if ($x == 13) {
		$x = 0;
		print "\n\t";
		}
	}

print "\}\;\n\nOk, done! Converted $x columns and $y rows.\n";